package dem1;

import java.util.Map;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-04-10
 * Time: 20:23
 */

class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head = null;
    public ListNode last = null;
    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (head != null){
            count++;
            head = head.next;
        }
        return count;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.println(cur.val);
            cur = cur.next;
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int data){
        if (head == null){
            head = new ListNode(data);
            last = head;
            return;
        }
        ListNode cur = new ListNode(data);
        cur.next = head;
        head.prev = cur;
        head = cur;
    }

    public void addLast(int data){
        ListNode cur = new ListNode(data);
        if (head == null){
            head = cur;
            last = cur;
            return;
        }
        last.next = cur;
        cur.prev = last;
        last = cur;
    }

}

public class Test1 {

    private static String bigNumSum(StringBuffer str1,StringBuffer str2){
        int min = 0;
        int max = 0;
        if(str1.length() > str2.length()){
            max = str1.length();
            min = str2.length();
        }else{
            min = str1.length();
            max = str2.length();
        }
        StringBuffer str = new StringBuffer(max + 1);
        int carry = 0;
        for(int i = 0; i < min; i++){
            carry += str1.charAt(i) + str2.charAt(i) - '0' - '0';
            str.append(carry % 10);
            carry /= 10;
        }
        for(int i = min; i < max; i++){
            carry += str2.charAt(i) - '0';
            str.append(carry % 10);
            carry /= 10;
        }
        if(carry != 0){
            str.append(carry);
        }
        return str.toString();
    }

    public static void main(String[] args) {
       StringBuffer stringBuffer = new StringBuffer();
       StringBuffer stringBuffer1 = new StringBuffer();
       stringBuffer.append("9999999");
       stringBuffer1.append("999");
       String str = bigNumSum(stringBuffer1,stringBuffer);
       System.out.println(str);
    }
}
